﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_DAL;
using Zor_Modes;
using Zor_Tools;

namespace Zor_Bll
{
    public class Zor_Summary_Bll<T> where T : class
    {
        /// <summary>
        /// 获取汇总管理列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageList(dynamic Model, int index, out int TotalCount, string where)
        {                      
            return PagerHelp<T>.GetPagerList(index, "V_Zor_Disannul", "Dn_Date desc", out TotalCount, GetSearchWhere(Model, where));
        }
        /// <summary>
        /// 获取未汇总管理列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> MGetPageList(int index, out int TotalCount, string where)
        {
            return PagerHelp<T>.GetPagerList(index, "V_Zor_Summary", "Sm_Id", out TotalCount, where);
        }
           
        /// <summary>
        /// 汇总
        /// </summary> 
        /// <param name="str">传进来数组形式：受理单ｉｄ，汇总主键ｉｄ，汇总金额</param>             
        /// <returns></returns>
        public static BaseAnswer UpdateSummary(string [] str)
        {
            var Sm_Id = "";
            var accpid = "";
            int actualtax = 0;
            var Bdid = "";
            var Gssds = 0;
            var Fcs = 0;
            for (int i=0; i < str.Length; i++)
            {
                var bitem = str[i].Split('-');
                Sm_Id += bitem[0] + ",";//汇总id 
                accpid += bitem[1] + ",";//受理单ID
                Gssds = Gssds + int.Parse(bitem[4]);//汇总个人所得税
                Fcs = Fcs + int.Parse(bitem[5]);//汇总个房产税税
                actualtax = actualtax+int.Parse(bitem[2]);//汇总总额
                Bdid += bitem[3] + ",";//楼宇ID

            }
            BaseAnswer bar = Zor_Summary_DAL<T>.UpdateSummary(Sm_Id.TrimEnd(','), accpid.TrimEnd(','), Gssds , Fcs, actualtax , Bdid);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "汇总成功！" : "汇总失败！";
            return bar;
        }
        /// <summary>
        /// 取消汇总
        /// </summary>
        /// <param name="accpid">受理单ｉｄ</param>      
        /// <param name="Dn_Id">消号表id</param>
        /// 受理状态(0保存,1提交2被退回、3已评议4已评定5、已审核、6已缴税、7、已开票8、已消号、9已汇总)
        /// <returns></returns>
        public static BaseAnswer CancelSummary(int Dn_Id, string accpid)
        {
            BaseAnswer bar = Zor_Summary_DAL<T>.CancelSummary(Dn_Id, accpid);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "取消汇总成功！" : "取消汇总失败！";
            return bar;
        }
             
        /// <summary>
        /// 打印列表
        /// </summary>
        /// <returns></returns>
        public static List<T> Print_Summary(int where)
        {
            return Zor_Summary_DAL<T>.Print_Summary(where);
        }

        /// 打印份数
        /// </summary>
        /// <returns></returns>
        public static List<T> GetCopies(int Id)
        {
            return Zor_Summary_DAL<T>.GetCopies(Id);
        }
        /// <summary>
        /// 打印总金额
        /// </summary>
        /// <returns></returns>
        public static List<T> GetSumMoney(int Id)
        {
            return Zor_Summary_DAL<T>.GetSumMoney(Id);
        }
        /// <summary>
        /// 汇总时间
        /// </summary>
        /// <returns></returns>
        public static List<T> GetSm_Date(int Id)
        {
            return Zor_Summary_DAL<T>.GetSm_Date(Id);
        }
        #region 私有函数
        /// <summary>
        /// 查询条件验证
        /// </summary>
        /// <param name="Model"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        private static string GetSearchWhere(dynamic Model, string where)
        {

         
            var sqlSurch = string.Empty;
            if (Model.DnUser != null)
            {
                sqlSurch += " and DnUser like '%" + Model.DnUser + "%'";
            }
            if (Model.Dn_Date != null)
            {
                sqlSurch += " and Dn_Date >=" + TimeHelper.ToTimeStamp(DateTime.Parse(Model.Dn_Date.ToString())) + " and Dn_Date<=" + TimeHelper.ToTimeStamp(DateTime.Parse(Model.Dn_Date.ToString())) + "";
            }

            if (Model.DnState != null)
            {
                if (Model.DnState == "全部")
                {
                    sqlSurch += " and DnState  in ('待消号','已消号')";
                }
                else
                {
                    sqlSurch += " and DnState = '" + Model.DnState + "'";
                }    
            }
          
            return where + sqlSurch;
        }
        #endregion
    }
}
